home *** CD-ROM | disk | FTP | other *** search
/ Click Press Kit / Click Press Kit.iso / pc / main.dxr / Internal_39_xml.node.ls < prev    next >
Encoding:
Text File  |  2006-05-31  |  3.6 KB  |  166 lines

  1. property name, type, text, child, count, attributeName, attributeValue, isEmpty
  2.  
  3. on new me, _name, _type, _data
  4.   me.name = _name
  5.   me.type = _type
  6.   me.text = _data
  7.   me.child = []
  8.   me.attributeName = []
  9.   me.attributeValue = [:]
  10.   return me
  11. end
  12.  
  13. on makeSublist me
  14.   case me.type of
  15.     #element:
  16.       xmlList = [:]
  17.       xmlList.addProp(me.name, me.toList())
  18.     #procInst:
  19.       xmlList = [:]
  20.       xmlList.addProp("!PROCINST", me.toList())
  21.     #charData:
  22.       xmlList = [:]
  23.       xmlList.addProp("!CHARDATA", me.toList())
  24.   end case
  25.   return xmlList
  26. end
  27.  
  28. on getAt me, index
  29.   if index.ilk = #integer then
  30.     return me.child[index]
  31.   else
  32.     index = string(index)
  33.     repeat with node in me.child
  34.       if node.name = index then
  35.         return node
  36.       end if
  37.     end repeat
  38.   end if
  39. end
  40.  
  41. on count me
  42.   return count(me.child)
  43. end
  44.  
  45. on getElementByName me, tagName, nodeList
  46.   if nodeList.ilk = #void then
  47.     nodeList = []
  48.   end if
  49.   repeat with node in me.child
  50.     if node.type = #element then
  51.       if node.name = tagName then
  52.         nodeList.add(node)
  53.       end if
  54.       node.getElementByName(tagName, nodeList)
  55.     end if
  56.   end repeat
  57.   if nodeList.count = 1 then
  58.     return nodeList[1]
  59.   else
  60.     return nodeList
  61.   end if
  62. end
  63.  
  64. on getText me
  65.   case me.type of
  66.     #charData:
  67.       return me.text
  68.     #element:
  69.       str = EMPTY
  70.       repeat with node in me.child
  71.         str = str & node.getText()
  72.       end repeat
  73.       return str
  74.     #procInst:
  75.       return me.text
  76.   end case
  77. end
  78.  
  79. on toList me
  80.   case me.type of
  81.     #element:
  82.       elemList = [:]
  83.       attrList = duplicate(attributeValue)
  84.       elemList.addProp("!ATTRIBUTES", attrList)
  85.       repeat with i in child
  86.         case i.type of
  87.           #element:
  88.             elemList.addProp(i.name, i.toList())
  89.           #charData:
  90.             elemList.addProp("!CHARDATA", i.toList())
  91.           #procInst:
  92.             elemList.addProp("!PROCINST", i.toList())
  93.         end case
  94.       end repeat
  95.       return elemList
  96.     #charData:
  97.       return me.text
  98.     #procInst:
  99.       piList = [:]
  100.       piList.addProp("NAME", me.name)
  101.       piList.addProp("TEXT", me.text)
  102.       return piList
  103.   end case
  104. end
  105.  
  106. on toString me
  107.   case me.type of
  108.     #element:
  109.       if me.isEmpty then
  110.         tag = "<" & me.name & me.attrString() & "/>"
  111.       else
  112.         tag = "<" & me.name & me.attrString() & ">"
  113.         repeat with node in me.child
  114.           tag = tag & node.toString()
  115.         end repeat
  116.         tag = tag & "</" & me.name & ">"
  117.       end if
  118.       return tag
  119.     #charData:
  120.       return me.encodeString(me.text)
  121.     #procInst:
  122.       return "<?" & me.name && me.text & "?>"
  123.   end case
  124. end
  125.  
  126. on attrString me
  127.   numAttr = count(me.attributeValue)
  128.   attrStr = EMPTY
  129.   if numAttr then
  130.     repeat with i = 1 to numAttr
  131.       attrName = attributeValue.getPropAt(i)
  132.       attrValue = me.encodeString(attributeValue[i])
  133.       attrStr = attrStr && attrName & "=" & QUOTE & attrValue & QUOTE
  134.     end repeat
  135.   end if
  136.   return attrStr
  137. end
  138.  
  139. on encodeString me, str
  140.   encodedStr = EMPTY
  141.   numChars = str.length
  142.   repeat with i = 1 to numChars
  143.     currChar = str.char[i]
  144.     case currChar of
  145.       "<":
  146.         encodedStr = encodedStr & "<"
  147.       ">":
  148.         encodedStr = encodedStr & ">"
  149.       "'":
  150.         encodedStr = encodedStr & "'"
  151.       QUOTE:
  152.         encodedStr = encodedStr & """
  153.       "&":
  154.         encodedStr = encodedStr & "&"
  155.       otherwise:
  156.         charNum = charToNum(currChar)
  157.         if charNum > 127 then
  158.           encodedStr = encodedStr & "&#" & charNum & ";"
  159.         else
  160.           encodedStr = encodedStr & currChar
  161.         end if
  162.     end case
  163.   end repeat
  164.   return encodedStr
  165. end
  166.